<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
<!--      <el-form-item label="就诊人ID" prop="patientId">
        <el-input
          v-model="queryParams.patientId"
          placeholder="请输入就诊人ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="就诊人姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入就诊人姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="录入医生ID" prop="doctorId">
        <el-input
          v-model="queryParams.doctorId"
          placeholder="请输入录入医生ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="录入医生姓名" prop="doctorName">
        <el-input
          v-model="queryParams.doctorName"
          placeholder="请输入录入医生姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>-->
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
            type="danger"
            plain
            :disabled="isEmpty(checkedIds)"
            @click="handleDeleteBatch"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
    >
    <el-table-column type="selection" width="55" />
      <el-table-column label="ID" align="center" prop="id" />
      <!--      <el-table-column label="就诊人ID" align="center" prop="patientId" />-->
      <el-table-column label="就诊人" align="center" prop="patientName" />
      <!--      <el-table-column label="录入医生ID" align="center" prop="doctorId" />-->
      <el-table-column label="医生姓名" align="center" prop="doctorName" />
      <el-table-column label="化疗次数" align="center" prop="num">
        <template #default="{row}">
          <p>第 {{row.num}} 次化疗</p>
        </template>
      </el-table-column>
      <el-table-column
        label="化疗时间"
        align="center"
        prop="hlDate"
        :formatter="dateFormatter"
        width="180px"
      />
<!--      <el-table-column label="ALT" align="center" prop="alt" />
      <el-table-column label="AST" align="center" prop="ast" />
      <el-table-column label="Cr" align="center" prop="cr" />
      <el-table-column label="BUN" align="center" prop="bun" />
      <el-table-column label="PSA" align="center" prop="psa" />
      <el-table-column label="睾酮" align="center" prop="testosterone" />
      <el-table-column label="WBC" align="center" prop="wbc" />
      <el-table-column label="Hb" align="center" prop="hb" />
      <el-table-column label="Plt" align="center" prop="plt" />
      <el-table-column label="用药" align="center" prop="useMedication" />
      <el-table-column label="多西他赛" align="center" prop="dxts" />
      <el-table-column label="其它" align="center" prop="otherData" />
      <el-table-column label="副作用-恶心-分级" align="center" prop="fzyExFj" />
      <el-table-column label="副作用-恶心-辅助用药" align="center" prop="fzyExFzyy" />
      <el-table-column label="副作用-恶心-恢复" align="center" prop="fzyExHf" />
      <el-table-column label="副作用-呕吐-分级" align="center" prop="fzyOtFj" />
      <el-table-column label="副作用-呕吐-辅助用药" align="center" prop="fzyOtFzyy" />
      <el-table-column label="副作用-呕吐-恢复" align="center" prop="fzyOtHf" />
      <el-table-column label="副作用-腹泻-分级" align="center" prop="fzyFxFj" />
      <el-table-column label="副作用-腹泻-辅助用药" align="center" prop="fzyFxFzyy" />
      <el-table-column label="副作用-腹泻-恢复" align="center" prop="fzyFxHf" />
      <el-table-column label="副作用-外周神经毒性-分级" align="center" prop="fzyWzsjdxFj" />
      <el-table-column label="副作用-外周神经毒性-辅助用药" align="center" prop="fzyWzsjdxFzyy" />
      <el-table-column label="副作用-外周神经毒性-恢复" align="center" prop="fzyWzsjdxHf" />
      <el-table-column label="副作用-骨髓抑制-分级" align="center" prop="fzyGsyzFj" />
      <el-table-column label="副作用-骨髓抑制-辅助用药" align="center" prop="fzyGsyzFzyy" />
      <el-table-column label="副作用-骨髓抑制-恢复" align="center" prop="fzyGsyzHf" />
      <el-table-column label="副作用-液体潴留-分级" align="center" prop="fzyYtzlFj" />
      <el-table-column label="副作用-液体潴留-辅助用药" align="center" prop="fzyYtzlFzyy" />
      <el-table-column label="副作用-液体潴留-恢复" align="center" prop="fzyYtzlHf" />
      <el-table-column label="副作用-其它-分级" align="center" prop="fzyOtherFj" />
      <el-table-column label="副作用-其它-辅助用药" align="center" prop="fzyOtherFzyy" />
      <el-table-column label="副作用-其它-恢复" align="center" prop="fzyOtherHf" />-->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <PatientHlForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { PatientHlApi, PatientHl } from '@/api/business/patienthl'
import PatientHlForm from './PatientHlForm.vue'

/** 化疗 列表 */
defineOptions({ name: 'PatientHl' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<PatientHl[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  patientId: undefined,
  patientName: undefined,
  doctorId: undefined,
  doctorName: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PatientHlApi.getPatientHlPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await PatientHlApi.deletePatientHl(id)
    message.success(t('common.delSuccess'))
    currentRow.value = {}
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除化疗 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await PatientHlApi.deletePatientHlList(checkedIds.value);
    checkedIds.value = [];
    message.success(t('common.delSuccess'))
    await getList();
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: PatientHl[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PatientHlApi.exportPatientHl(queryParams)
    download.excel(data, '化疗.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const route = useRoute() // 路由
const router = useRouter() // 路由

/** 初始化 **/
onMounted(() => {
  // 如果就诊人 ID 不存在，显示错误提示并关闭页面
  if (!route.query.id) {
    message.error('就诊人 ID 不存在，无法查看就诊记录')
    // 关闭当前路由，返回到就诊人列表页面
    router.push({ name: 'UserPatient' })
    return
  }

  queryParams.patientId = route.query.id
  getList()
})
</script>
